package com.ycu.jk16.mall.controller.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ycu.jk16.mall.domain.Category;
import com.ycu.jk16.mall.domain.ParentCategory;
import com.ycu.jk16.mall.service.CategoryService;
import com.ycu.jk16.mall.util.common.ResponseJsonUtils;

/**
 * 
 * @ClassName AdminCategoryController
 * @Description TODO(AdminCategoryController)
 * @author 倪宗龙
 * @date 2018年12月11日 下午1:44:01
 * @version V1.0
 */
@Controller
@RequestMapping(value = "/manage/category")
public class AdminCategoryController {
	@Autowired
	@Qualifier("categoryService")
	private CategoryService categoryService;

	/**
	 * 
	 * @Title getCategory @Description TODO(获取品类子节点(平级)) @param @param
	 *        id @param @param response @return void @throws
	 */
	@RequestMapping(value = "/get_category")
	public void getCategory(Integer categoryId, HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();

		try {
			// catch测试块
			Category category = categoryService.findCategoryById(categoryId);
			ParentCategory parentCategory = category.getParentCategory();
			List<Category> list = categoryService.getDeepCategory(parentCategory.getId());
			list.get(0);

			// 加入json数据
			data.put("status", 0);
			data.put("data", list);
		} catch (NullPointerException e) {
			data.put("status", 1);
			data.put("msg", "找不到该品类");
		} finally {
			ResponseJsonUtils.json(response, data);
		}
	}

	/**
	 * 
	 * @Title addCategory @Description TODO(增加品类节点) @param @param
	 *        parentId @param @param categoryName @param @param response @return
	 *        void @throws
	 */
	@RequestMapping(value = "/add_category")
	public void addCategory(Integer parentId, String categoryName, HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		try {
			// 添加品类
			categoryService.addCategory(parentId, categoryName);

			data.put("status", 0);
			data.put("msg", "品类添加成功");
		} catch (Exception e) {
			data.put("status", 1);
			data.put("msg", "品类添加失败");
		} finally {
			ResponseJsonUtils.json(response, data);
		}
	}

	/**
	 * 
	 * @Title setCategoryName @Description TODO(修改品类名字) @param @param
	 *        categoryId @param @param categoryName @param @param
	 *        response @return void @throws
	 */
	@RequestMapping(value = "/set_category_name")
	public void setCategoryName(Integer categoryId, String categoryName, HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		Category category = categoryService.findCategoryById(categoryId);

		try {
			// 修改名字
			category.setName(categoryName);
			categoryService.updateName(category);

			// 加入json数据
			data.put("status", 0);
			data.put("msg", "更新品类名字成功");
		} catch (NullPointerException e) {
			data.put("status", 1);
			data.put("msg", "更新品类名字失败");
		} finally {
			ResponseJsonUtils.json(response, data);
		}
	}

	/**
	 * 
	 * @Title getDeepCategory @Description
	 *        TODO(获取当前分类id及递归子节点categoryId) @param @param
	 *        categoryId @param @param response @return void @throws
	 */
	@RequestMapping(value = "/get_deep_category")
	public void getDeepCategory(Integer categoryId, HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		Category category = categoryService.findCategoryById(categoryId);
		List<Category> list = categoryService.getDeepCategory(category.getParentCategory().getId());
		List<Integer> idList = new ArrayList<>();
		try {
			// catch测试块
			list.get(0);

			// 获取id集合
			for (int i = 0; i < list.size(); i++) {
				idList.add(list.get(i).getId());
			}

			// 加入json数据
			data.put("status", 0);
			data.put("data", idList);
		} catch (NullPointerException e) {
			data.put("status", 1);
			data.put("msg", "无权限");
		} finally {
			ResponseJsonUtils.json(response, data);
		}
	}

	@RequestMapping(value = "/add_parentCategory")
	public void add_parentCategory(String name, HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();

		try {
			categoryService.addParentCategory(name);
			ParentCategory parentCategory = categoryService.findParentCategoryByName(name);
			// 加入json数据
			data.put("status", 0);
			data.put("data", parentCategory);
		} catch (NullPointerException e) {
			data.put("status", 1);
			data.put("msg", "更新品类名字失败");
		} finally {
			ResponseJsonUtils.json(response, data);
		}
	}

	@RequestMapping(value = "/set_parentCategory")
	public void set_parentCategory(Integer id, String name, HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();

		try {
			categoryService.updateParentCategory(id, name);
			// 加入json数据
			data.put("status", 0);
			data.put("msg", "设置父类名称成功");
		} catch (NullPointerException e) {
			data.put("status", 1);
			data.put("msg", "更新失败");
		} finally {
			ResponseJsonUtils.json(response, data);
		}
	}

	@RequestMapping(value = "/get_parentCategory_list")
	public void get_parentCategory_list(HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();

		try {
			List<ParentCategory> parentCategorys = categoryService.getParentCategorieList();
			// 加入json数据
			data.put("status", 0);
			data.put("data", parentCategorys);
		} catch (NullPointerException e) {
			data.put("status", 1);
			data.put("msg", "更新失败");
		} finally {
			ResponseJsonUtils.json(response, data);
		}
	}

	@RequestMapping(value = "/get_category_by_parent")
	public void get_category_by_parent(Integer parentId, HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();

		try {
			List<Category> categorys = categoryService.getCategoriesByParentId(parentId);
			// 加入json数据
			data.put("status", 0);
			data.put("data", categorys);
		} catch (NullPointerException e) {
			data.put("status", 1);
			data.put("msg", "获取失败，没有此父类");
		} finally {
			ResponseJsonUtils.json(response, data);
		}
	}
}
